package cn.gwm.flink.streaming.dwm.charge.util;

import cn.gwm.flink.streaming.constant.BusinessUrl;
import cn.gwm.utils.ConfigLoader;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.LRUMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liangguang
 * @Date: 2022/6/9 车型四码
 * @Description:
 */
@Slf4j
public class ModeCodeUtil {
    /**
     * k -device
     * v - 需要的字段
     * 考虑一个问题：如果任务一直存在则modeMap 会持续增加，占用内存不释放
     * 此处考虑 给一个限定大小的map，给定map size的上限
     * 暂定给 1024*16 =16384  【2048辆车】
     * 此处，如果超过 16384辆车同时传输数据，则会达到上限，同时掉用接口
     */
    private static final Map<String, Map<String, String>> modeMap = new LRUMap(2 * 1024);
    /**
     * 更新间隔时间，给随机数是为了防止系统中别的请求设定同一时间对请求系统造成并发压力
     */
//    private static final Long LONG_TIME = 3 * 60 * 60 * 1000L + RandomUtil.randomLong(20000);
    private static final Long LONG_TIME = 3000L;

    private static final String UPDATE_TIME = "update_time";

    public static void main(String[] args) {
        System.out.println(batteryData("LGWEEUA51NE203358"));
    }


    /**
     * 查询 电池数据
     *
     * @param vin
     * @return
     */
    public static Map<String, String> batteryData(String vin) {
        if (!modeMap.containsKey(vin)) {
            queryData(vin);
        } else {
            Map<String, String> vinMap = modeMap.get(vin);
            if (System.currentTimeMillis() - Long.valueOf(vinMap.get(UPDATE_TIME)) > LONG_TIME) {
                // 需要重新更新
                queryData(vin);
            }
        }
        return modeMap.get(vin);
    }

    public static void queryData(String vin) {
        Map<String, Object> param = new HashMap<>();
        param.put("vin", vin);
        Map<String, String> result = new HashMap<>();
        result.put(UPDATE_TIME, System.currentTimeMillis() + "");
        try {
            String s = HttpUtil.get(ConfigLoader.get("charging.station.type.url") + BusinessUrl.BATTERY_DATA_URL, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.containsKey("code") && parseObj.getInt("code") == 200) {
                JSONObject resultData = parseObj.getJSONObject("data");
                result.put("modelCode", resultData.getStr("vehModelName"));
                result.put("cellBatteryType", resultData.getStr("cellBatteryType"));
                result.put("modelName", resultData.getStr("modelName"));
                result.put("licensePlate", resultData.getStr("licensePlate"));
                result.put("packCapacity", resultData.getStr("packCapacity"));
                result.put("ownerName", resultData.getStr("ownerName"));
                result.put("productMaterialCode", resultData.getStr("productMaterialCode"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" ModeCodeUtil batteryData happen error :" + e.getMessage() + "StackTrace={}", e.getStackTrace());
        }
        modeMap.put(vin, result);
    }
}
